package conson.tech.manage.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.manage.api.app.constant.AppCommon;
import conson.tech.manage.app.entity.pojo.AppConfig;
import conson.tech.manage.app.entity.pojo.AppGroupConfig;
import conson.tech.manage.app.entity.pojo.AppPageConfig;
import conson.tech.manage.app.entity.vo.AppPageConfigVo;
import conson.tech.manage.app.entity.vo.PageVo;
import conson.tech.manage.app.mapper.AppConfigMapper;
import conson.tech.manage.app.mapper.AppGroupConfigMapper;
import conson.tech.manage.app.mapper.AppPageConfigMapper;
import conson.tech.manage.app.service.AppPageConfigService;
import conson.tech.manage.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AppPageConfigServiceImpl
 * Description:
 * Author: HXL
 * Date: 2020/4/23 2:18 下午
 * Version: 1.0
 */
@Service("appPageConfigService")
public class AppPageConfigServiceImpl implements AppPageConfigService {

    @Autowired
    private AppPageConfigMapper appPageConfigMapper;
    @Autowired
    private AppGroupConfigMapper appGroupConfigMapper;
    @Autowired
    private AppConfigMapper appConfigMapper;

    @Override
    public Page<AppPageConfigVo> queryPageList(PageVo vo) {

        Page<AppPageConfig> page = new Page();
        page.setCurrent(vo.getPageIndex());
        page.setSize(vo.getPageSize());
        Integer pageType = null;
        //页面请求类型:1-首页默认应用 2-应用配置（全部应用管理）3-首页模块  4-生活页模块 5-信豆页引导模块 6-功能专题页模块
        if (vo.getPageRequestType() != null){
            if (vo.getPageRequestType() == 3){
                pageType = 0;
            }else if (vo.getPageRequestType() == 4){
                pageType = 1;
            }
        }
        Page<AppPageConfig> pageConfigPage = appPageConfigMapper.queryPageList(page,pageType);

        Page<AppPageConfigVo> appPageConfigVoPage = new Page<>();
        if (pageConfigPage != null) {
            List<AppPageConfig> records = pageConfigPage.getRecords();
            if (!CollectionUtils.isEmpty(records)){
                for (AppPageConfig record : records) {
                    QueryWrapper groupWrapper = new QueryWrapper();
                    AppGroupConfig appGroup = new AppGroupConfig();
                    appGroup.setAppPageConfigId(record.getId());
                    appGroup.setDelFlag(AppCommon.APP_DEL_FLAG_ONE);
                    groupWrapper.setEntity(appGroup);
                    groupWrapper.orderByAsc("sort");
                    List<AppGroupConfig> groupList = appGroupConfigMapper.selectList(groupWrapper);
                    if (!CollectionUtils.isEmpty(groupList)){
                        List<AppConfig> appConfigList = new ArrayList<>();
                        for (AppGroupConfig appGroupConfig : groupList) {
                            QueryWrapper appWrapper = new QueryWrapper();
                            AppConfig appConfig = new AppConfig();
                            appConfig.setGroupId(appGroupConfig.getId());
                            appConfig.setDelFlag(AppCommon.APP_DEL_FLAG_ONE);
                            appWrapper.setEntity(appConfig);
                            appWrapper.orderByAsc("sort");
                            appConfigList.addAll(appConfigMapper.selectList(appWrapper));
                            //appGroupConfig.setAppConfigList( appConfigMapper.selectList(appWrapper));
                        }
                        record.setAppConfigList(appConfigList);
                    }
                    record.setAppGroupConfigList(groupList);
                }
            }
            return EntityUtils.pageToPage(pageConfigPage, AppPageConfigVo.class);
        }
        return appPageConfigVoPage;
    }
    @Transactional
    @Override
    public Long insertAppPageConfig(AppPageConfig pageConfig) {
        pageConfig.setCreatedTime(LocalDateTime.now());
        pageConfig.setUpdatedTime(LocalDateTime.now());
        appPageConfigMapper.insertAppPageConfig(pageConfig);
        return pageConfig.getId();
    }

    @Transactional
    @Override
    public int deleteAppPageConfig(Long moduleId) {
        int i = appPageConfigMapper.deletePageConfigById(moduleId);
        if (i >0){
            QueryWrapper queryWrapper = new QueryWrapper();
            AppGroupConfig appGroupConfig = new AppGroupConfig();
            appGroupConfig.setAppPageConfigId(moduleId);
            queryWrapper.setEntity(appGroupConfig);
            List<AppGroupConfig> list = appGroupConfigMapper.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(list)){
                for (AppGroupConfig groupConfig : list) {
                    appGroupConfigMapper.deleteAppGroupConfigById(groupConfig.getId());
                    appConfigMapper.deleteAppConfigByGroupId(groupConfig.getId());
                }
            }
        }
        return 0;
    }

    @Override
    public List<AppPageConfig> queryAppPageConfigByPageType(int pageType,int versionValue) {

        return appPageConfigMapper.queryAppPageConfigByPageType(pageType,versionValue);
    }

    @Override
    public int updateAppPageConfig(AppPageConfig pageConfig) {
        return appPageConfigMapper.updateById(pageConfig);
    }
}
